Repository navigation
Fix C harness JSON output for non-finite doubles - #441
Conversation
jserv
left a comment
There was a problem hiding this comment.
Check CONTRIBUTING.md carefully, run make hooks, and rework git commit messages.
| static struct Node* nextTree(int* values, bool* nulls, int size) { if (!size || nulls[0]) return NULL; struct Node** nodes = calloc(size, sizeof(*nodes)); for (int i = 0; i < size; i++) if (!nulls[i]) { nodes[i] = calloc(1, sizeof(*nodes[i])); nodes[i]->val = values[i]; } for (int i = 0, child = 1; i < size && child < size; i++) { if (!nodes[i]) continue; if (child < size) nodes[i]->left = nodes[child++]; if (child < size) nodes[i]->right = nodes[child++]; } struct Node* root = nodes[0]; free(nodes); return root; } | ||
| static void json_int_array(FILE* out, const int* values, int size) { fputc('[', out); for (int i = 0; i < size; i++) { if (i) fputc(',', out); fprintf(out, "%d", values[i]); } fputc(']', out); } | ||
| static void json_double_array(FILE* out, const double* values, int size) { fputc('[', out); for (int i = 0; i < size; i++) { if (i) fputc(',', out); fprintf(out, "%.17g", values[i]); } fputc(']', out); } | ||
| static void json_double(FILE* out, double value) { if (isfinite(value)) fprintf(out, "%.17g", value); else fputs("null", out); } |
There was a problem hiding this comment.
The Python runner still has the bug this fixes for C. In web/runners.js the per-case json.dumps(actual) check only catches TypeError/ValueError, and the default allow_nan=True raises neither, so float("inf") reaches the final json.dumps({"results": results}) as bare Infinity and the worker's JSON.parse rejects the whole run. This can be a follow-up: map non-finite floats in actual to None before appending, and add the same powx-n case for Python.
d023cc2 to
b262ea6
Compare
Non-finite C answers produce invalid JSON and discard results for every case, including passing ones. Serialize them as null to keep the run readable and match the C++ and Java runners.
b262ea6 to
6f84dc7
Compare
|
Issue #438 reports that a C solution returning |
|
@jeng-a-yo #438 reports this exact failure, with the Compound Growth Factor reproduction, and is assigned to @peter1001136, who filed it. Since this branch delivers it, the assignee has been asked to review this pull request rather than open a second one. What stands between this branch and a re-review is the |
|
Thank @jeng-a-yo for contributing! |
When a C solution returns inf or NaN, the harness prints it directly into the JSON output. This make the whole result invalid, so even passing cases disappear.
This change prints null for non-finite doubles, like the C++ and Java runners already do. It handles both double and double[].
I added four regression tests for infinity, negative infinity, NaN, and mixed arrays. They fail before the fix and pass afterward. All 66 tests in the compiler-explorer test file pass.
The full test suite still has some local failures due to missing assets, plus a C++ test that was terminated. Rerunning the compiler-explorer test file on its own passed.
Summary by cubic
Fixes the C harness so non-finite doubles (inf, -inf, NaN) serialize as
nullinstead of rawinf/nanstrings, which were corrupting the entire JSON output and hiding passing results. This makes the C runner consistent with the existing C++ and Java runners. Applies to bothdoubleanddouble[]return types, and adds regression tests for all non-finite cases plus mixed finite/non-finite arrays.Written for commit 6f84dc7. Summary will update on new commits.
Closes #438